/*
 * ProjectViewer.java
 *
 * Created on January 11, 2008, 7:55 PM
 */

package picto.tools.viewer;

import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.image.BufferedImage;
import java.util.*;
import java.util.prefs.*;
import javax.swing.*;
import picto.tools.*;
import java.io.*;
import java.text.MessageFormat;
import picto.io.*;
import picto.core.*;
import picto.draw.DrawCanvas;
import picto.tools.AbstractTool.QuitAction;
import picto.util.Resources;

/**
 *
 * @author  Matt
 */
public class ProjectViewer extends AbstractTool {
    
    private static Preferences prefs = Preferences.userNodeForPackage(ProjectViewer.class);
    
    private static ResourceBundle bundle = AbstractTool.loadBundle(
                        ProjectViewer.class.getPackage().getName()+".gui",
                        false, "Picto Error", "Cannot find program text. Continue?");
    
    protected static String getString(String key, String def) {
        return Resources.getString(bundle, key, def);
    }
    
    protected static String getString(String key) {
        return Resources.getString(bundle, key);
    }
    
    protected static Preferences getPreferences() {
        return prefs;
    }
    
    private ArchiveIO archiver = new ArchiveIO();
    private Project project = null;
    private BufferedImage thumb = null;
    
    private String lastFile = null;
            
    private String emptyStr       = getString("view.info.empty");
    private String headerStr      = getString("view.info.header");
    private String pressureStr    = getString("view.info.pressure");
    private String opacityStr     = getString("view.info.opacity");
    private String layersStr      = getString("view.info.layers");
    private String snapshotsStr   = getString("view.info.snapshots");
    private String brushesStr     = getString("view.info.brushes");
    
    private String trueStr        = getString("boolean.true");
    private String falseStr       = getString("boolean.false");
    private String emptyTitleStr  = getString("empty.title");
    private String emptyAuthorStr = getString("empty.author");
    
    /**
     * Creates new form ProjectViewer
     */
    public ProjectViewer() {
        initComponents();
        setup(null);
        
        Runtime.getRuntime().addShutdownHook(new Thread() {
            { setName("ProjectViewer Cleanup"); }
            public void run() {
                cleanup();
            }
        });
    }
    
    protected Preferences prefs() {
        return prefs;
    }
    
    private void resizeThumb() {
        
    }
    
    private void loadNewArchive() {
        String path = pathField.getText();
        if (path==null || path.trim().length()==0)
            return;
        File file = new File(path);
        try {            
            if (file.exists()) {
                Project proj = new Project();
                ArchiveReader r = archiver.read(proj, new FileInputStream(file));
                
                ArchiveEntry e = r.getEntry(ArchiveEntry.XML_ENTRY_NAME);
                if (e!=null) {
                    InputStream in = e.openStream();

                    BufferedReader bin = new BufferedReader(new InputStreamReader(in));
                    try {
                        StringBuffer buffer = new StringBuffer();
                        String line = "";
                        while ((line = bin.readLine()) != null) {
                            buffer.append(line+"\n");
                        }
                        try { bin.close(); } catch (Exception exc) {}
                        try { in.close(); } catch (Exception exc) {}

                        xmlTextArea.setText(buffer.toString());
                    } catch (IOException ex) {
                        xmlTextArea.setText("Error reading XML: "+ex.getMessage());
                    }
                }
                
                setup(proj);
            } else
                throw new FileNotFoundException();
        } catch (Exception e) {
            String msg;
            if (e instanceof FileNotFoundException)
                msg = getString("err.nofile");
            else
                msg = getString("err.read");
            msg = MessageFormat.format(msg, file.getName());
            String title = getString("err.title");
            JOptionPane.showMessageDialog(this, msg, title, JOptionPane.ERROR_MESSAGE);
        }
    }
    
    private void setupThumb(LayerGroup group) {
        int width = project.getWidth();
        int height = project.getHeight();
        thumb = new BufferedImage(width, height, BufferedImage.TYPE_4BYTE_ABGR);
        Graphics2D g2d = thumb.createGraphics();
        for (int i=0; i<group.size(); i++) {
            group.get(i).paint(g2d);
        }
    }
    
    protected JFileChooser initJFileChooser(String dir) {
        JFileChooser jfc = super.initJFileChooser(dir);
        jfc.setFileFilter(new ProjectFileFilter());
        return jfc;
    }
    
    private void setup(Project project) {
        this.project = project;
        
        thumb = null;
        String header = emptyStr;
        String pressure = emptyStr;
        String opacity = emptyStr;
        String brushes = emptyStr;
        String snapshots = emptyStr;
        String layers = emptyStr;
        String markupText = null;
        
        if (project != null) {
            String title = project.getTitle();
            if (title==null||title.trim().length()==0)
                title = emptyTitleStr;
            String author = project.getAuthor();
            if (author==null||author.trim().length()==0)
                author = emptyAuthorStr;
            
            header = MessageFormat.format(headerStr, title, author);
            
            BrushPack pack = project.getBrushPack();
            int brushCount = 0;
            if (pack!=null)
                brushCount = pack.size();
            brushes = MessageFormat.format(brushesStr, brushCount);
            
            Snapshots snaps = project.getSnapshots();
            int snapsCount = 0;
            if (snaps!=null)
                snapsCount = snaps.size();
            snapshots = MessageFormat.format(snapshotsStr, snapsCount);
            
            LayerGroup group = project.getLayerGroup();
            int layerCount = 0;
            if (group!=null)
                layerCount = group.size();
            layers = MessageFormat.format(layersStr, layerCount);
            if (layerCount>0)
                setupThumb(group);
            
            Project.Options options = project.getOptions();
            float opac = options.getBrushOpacity();
            opacity = MessageFormat.format(opacityStr, opac*100, opac);
            
            boolean press = options.isPressureEnabled();
            String boolStr = press ? trueStr : falseStr;
            pressure = MessageFormat.format(pressureStr, boolStr);
        }
        
        headerLabel.setText(header);
        pressureLabel.setText(pressure);
        opacityLabel.setText(opacity);
        brushesLabel.setText(brushes);
        snapshotsLabel.setText(snapshots);
        layersLabel.setText(layers);
        
        repaint();
    }
    
    public class ProjectFileFilter extends BasicFileFilter {
        public static final String EXT = ".picto";
        
        public ProjectFileFilter() {
            super(MessageFormat.format(
                            getString("file.picto.description"),
                            EXT), EXT);
        }
    }
        
    public class ThumbCanvas extends JPanel {
        
        private final int CHECKER_SIZE = 10;
        
        public void paintComponent(Graphics g) {
            super.paintComponent(g);
            
            int width = getWidth();
            int height = getHeight();
            DrawCanvas.drawCheckeredBackground(g, width, height, CHECKER_SIZE);
            
            if (thumb!=null) {
                Graphics2D g2d = (Graphics2D)g;
                g2d.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_NEAREST_NEIGHBOR);
                
                int tw = (int)(thumb.getWidth() * (height/(float)thumb.getHeight()));
                int th = height;
                tw = Math.min(tw, thumb.getWidth());
                th = Math.min(th, thumb.getHeight());
                g.drawImage(thumb, 0, 0, tw, th, null);
            }
        }
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
    private void initComponents() {
        pathPanel = new javax.swing.JPanel();
        pathField = new javax.swing.JTextField();
        pathBrowse = new javax.swing.JButton();
        pathLoad = new javax.swing.JButton();
        viewPanel = new javax.swing.JPanel();
        tabbedPane = new javax.swing.JTabbedPane();
        infoPanel = new javax.swing.JPanel();
        headerLabel = new javax.swing.JLabel();
        opacityLabel = new javax.swing.JLabel();
        brushesLabel = new javax.swing.JLabel();
        snapshotsLabel = new javax.swing.JLabel();
        layersLabel = new javax.swing.JLabel();
        pressureLabel = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        thumbCanvas = new ThumbCanvas();
        xmlPanel = new javax.swing.JPanel();
        xmlScrollPane = new javax.swing.JScrollPane();
        xmlTextArea = new javax.swing.JTextArea();
        menuBar = new javax.swing.JMenuBar();
        fileMenu = new javax.swing.JMenu();
        quitItem = new javax.swing.JMenuItem();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle(getString("app.title"));
        pathPanel.setBorder(javax.swing.BorderFactory.createTitledBorder(getString("path.border")));
        pathField.setText(getString("path.field"));
        pathField.setToolTipText(getString("path.field.tip"));
        pathField.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                pathFieldActionPerformed(evt);
            }
        });

        pathBrowse.setText(getString("path.browse"));
        pathBrowse.setToolTipText(getString("path.browse.tip"));
        pathBrowse.setRequestFocusEnabled(false);
        pathBrowse.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                pathBrowseActionPerformed(evt);
            }
        });

        pathLoad.setText(getString("path.load"));
        pathLoad.setToolTipText(getString("path.load.tip"));
        pathLoad.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                pathLoadActionPerformed(evt);
            }
        });

        org.jdesktop.layout.GroupLayout pathPanelLayout = new org.jdesktop.layout.GroupLayout(pathPanel);
        pathPanel.setLayout(pathPanelLayout);
        pathPanelLayout.setHorizontalGroup(
            pathPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(pathPanelLayout.createSequentialGroup()
                .addContainerGap()
                .add(pathPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(pathPanelLayout.createSequentialGroup()
                        .add(pathField, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 380, Short.MAX_VALUE)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(pathBrowse))
                    .add(pathLoad))
                .addContainerGap())
        );
        pathPanelLayout.setVerticalGroup(
            pathPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(pathPanelLayout.createSequentialGroup()
                .addContainerGap()
                .add(pathPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(pathBrowse)
                    .add(pathField, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(pathLoad)
                .addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        tabbedPane.setMinimumSize(new java.awt.Dimension(250, 250));
        headerLabel.setText(getString("view.info.empty"));

        opacityLabel.setHorizontalAlignment(javax.swing.SwingConstants.TRAILING);
        opacityLabel.setText(getString("view.info.empty"));

        brushesLabel.setHorizontalAlignment(javax.swing.SwingConstants.TRAILING);
        brushesLabel.setText(getString("view.info.empty"));

        snapshotsLabel.setHorizontalAlignment(javax.swing.SwingConstants.TRAILING);
        snapshotsLabel.setText(getString("view.info.empty"));

        layersLabel.setHorizontalAlignment(javax.swing.SwingConstants.TRAILING);
        layersLabel.setText(getString("view.info.empty"));

        pressureLabel.setHorizontalAlignment(javax.swing.SwingConstants.TRAILING);
        pressureLabel.setText(getString("view.info.empty"));

        thumbCanvas.setMinimumSize(new java.awt.Dimension(50, 50));
        thumbCanvas.setOpaque(false);
        thumbCanvas.addComponentListener(new java.awt.event.ComponentAdapter() {
            public void componentResized(java.awt.event.ComponentEvent evt) {
                thumbCanvasComponentResized(evt);
            }
        });

        org.jdesktop.layout.GroupLayout thumbCanvasLayout = new org.jdesktop.layout.GroupLayout(thumbCanvas);
        thumbCanvas.setLayout(thumbCanvasLayout);
        thumbCanvasLayout.setHorizontalGroup(
            thumbCanvasLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(0, 363, Short.MAX_VALUE)
        );
        thumbCanvasLayout.setVerticalGroup(
            thumbCanvasLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(0, 211, Short.MAX_VALUE)
        );
        jScrollPane1.setViewportView(thumbCanvas);

        org.jdesktop.layout.GroupLayout infoPanelLayout = new org.jdesktop.layout.GroupLayout(infoPanel);
        infoPanel.setLayout(infoPanelLayout);
        infoPanelLayout.setHorizontalGroup(
            infoPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(infoPanelLayout.createSequentialGroup()
                .addContainerGap()
                .add(infoPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING, false)
                    .add(layersLabel, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .add(snapshotsLabel, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .add(brushesLabel, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 127, Short.MAX_VALUE)
                    .add(pressureLabel, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 127, Short.MAX_VALUE)
                    .add(opacityLabel, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(infoPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(headerLabel, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 363, Short.MAX_VALUE)
                    .add(jScrollPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 363, Short.MAX_VALUE))
                .addContainerGap())
        );
        infoPanelLayout.setVerticalGroup(
            infoPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(infoPanelLayout.createSequentialGroup()
                .addContainerGap()
                .add(headerLabel)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(infoPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(infoPanelLayout.createSequentialGroup()
                        .add(pressureLabel, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 14, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(opacityLabel, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 14, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(brushesLabel, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 14, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(snapshotsLabel)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(layersLabel))
                    .add(jScrollPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 211, Short.MAX_VALUE))
                .addContainerGap())
        );
        tabbedPane.addTab(getString("view.tab.info"), null, infoPanel, getString("view.tab.info.tip"));

        xmlTextArea.setColumns(20);
        xmlTextArea.setEditable(false);
        xmlTextArea.setRows(5);
        xmlScrollPane.setViewportView(xmlTextArea);

        org.jdesktop.layout.GroupLayout xmlPanelLayout = new org.jdesktop.layout.GroupLayout(xmlPanel);
        xmlPanel.setLayout(xmlPanelLayout);
        xmlPanelLayout.setHorizontalGroup(
            xmlPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(xmlPanelLayout.createSequentialGroup()
                .addContainerGap()
                .add(xmlScrollPane, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 496, Short.MAX_VALUE)
                .addContainerGap())
        );
        xmlPanelLayout.setVerticalGroup(
            xmlPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(xmlPanelLayout.createSequentialGroup()
                .addContainerGap()
                .add(xmlScrollPane, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 231, Short.MAX_VALUE)
                .addContainerGap())
        );
        tabbedPane.addTab(getString("view.tab.xml"), null, xmlPanel, getString("view.tab.xml.tip"));

        org.jdesktop.layout.GroupLayout viewPanelLayout = new org.jdesktop.layout.GroupLayout(viewPanel);
        viewPanel.setLayout(viewPanelLayout);
        viewPanelLayout.setHorizontalGroup(
            viewPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(viewPanelLayout.createSequentialGroup()
                .addContainerGap()
                .add(tabbedPane, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 521, Short.MAX_VALUE)
                .addContainerGap())
        );
        viewPanelLayout.setVerticalGroup(
            viewPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(viewPanelLayout.createSequentialGroup()
                .addContainerGap()
                .add(tabbedPane, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 281, Short.MAX_VALUE)
                .addContainerGap())
        );

        fileMenu.setText(getString("menu.file"));
        quitItem.setAction(new QuitAction());
        quitItem.setText(getString("menu.file.quit"));
        fileMenu.add(quitItem);

        menuBar.add(fileMenu);

        setJMenuBar(menuBar);

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .add(pathPanel, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
            .add(viewPanel, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(pathPanel, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(viewPanel, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void pathBrowseActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_pathBrowseActionPerformed
        File file = getFile(getString("file.browse"));
        if (file!=null) {
            pathField.setText(file.getAbsolutePath());
            loadNewArchive();
        }
    }//GEN-LAST:event_pathBrowseActionPerformed

    private void thumbCanvasComponentResized(java.awt.event.ComponentEvent evt) {//GEN-FIRST:event_thumbCanvasComponentResized
        
    }//GEN-LAST:event_thumbCanvasComponentResized

    private void pathLoadActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_pathLoadActionPerformed
        loadNewArchive();
    }//GEN-LAST:event_pathLoadActionPerformed

    private void pathFieldActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_pathFieldActionPerformed
        loadNewArchive();
    }//GEN-LAST:event_pathFieldActionPerformed
    
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        try { UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); }
        catch (Exception e) {}
        
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                ProjectViewer view = new ProjectViewer();
                view.setLocationRelativeTo(null);
                view.setVisible(true);
            }
        });
    }
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel brushesLabel;
    private javax.swing.JMenu fileMenu;
    private javax.swing.JLabel headerLabel;
    private javax.swing.JPanel infoPanel;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JLabel layersLabel;
    private javax.swing.JMenuBar menuBar;
    private javax.swing.JLabel opacityLabel;
    private javax.swing.JButton pathBrowse;
    private javax.swing.JTextField pathField;
    private javax.swing.JButton pathLoad;
    private javax.swing.JPanel pathPanel;
    private javax.swing.JLabel pressureLabel;
    private javax.swing.JMenuItem quitItem;
    private javax.swing.JLabel snapshotsLabel;
    private javax.swing.JTabbedPane tabbedPane;
    private javax.swing.JPanel thumbCanvas;
    private javax.swing.JPanel viewPanel;
    private javax.swing.JPanel xmlPanel;
    private javax.swing.JScrollPane xmlScrollPane;
    private javax.swing.JTextArea xmlTextArea;
    // End of variables declaration//GEN-END:variables
    
}

/*ArchivableImage aImg = new CachedImage("SpatterBrush", "res/brushes/brush1.png");
        JOptionPane.showMessageDialog(null, new ImageIcon(aImg.getImage()));
        
        Brush.setPressureEnabled(false);
        Brush.setOpacity(0.75f);
        
        BrushPack brushes = new BrushPack();
        
        Brush b = new Brush();
        b.setName("Fuzzy");
        b.setDiameter(10);
        b.setSmudge(false);
        b.setTexture(aImg);
        
        brushes.addBrush(b);
        
        Project state = new Project();
        state.setAuthor("davedes");
        state.setTitle("The Blob");
        state.setBrushPack(brushes);
        state.setSnapshots(new Snapshots());
        state.getSnapshots().add(aImg);
        state.setLayers(new LayerGroup());
        state.getLayerGroup().add(new Layer("test"));
        Layer l2 = new Layer("Foreground");
        l2.setImage(aImg);
        l2.setOpacity(0.25f);
        l2.setSelected(true);
        Layer l3 = new Layer("top");
        l2.setVisible(false);
        state.getLayerGroup().add(l2);
        state.getLayerGroup().add(l3);
        
        ArchiveIO io = new ArchiveIO();
        io.write(state, new FileOutputStream("outfile.zip"));
        Project state2 = new Project();
        io.read(state2, new FileInputStream("outfile.zip"));
        System.out.println(state2.getTitle());
        
        Brush b2 = state2.getBrushPack().brushes().get(0);
        Image image = b2.getTexture().getImage();
        JOptionPane.showMessageDialog(null, new ImageIcon(image));
*/